﻿/*****
 *  Copyright 2011, Kaven Gagné
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using Knowledge.DataModel;

namespace Knowledge
{
    public class RootViewModel : ViewModelBase
    {
        #region Constructors
        public RootViewModel() : base(null, true)
        {
            this.LoadFolders();
        }
        #endregion //Constructors

        #region Data
        private ObservableCollection<FolderViewModel> _folders;
        #endregion //Data
        
        #region Presentation Members

        #region Folders
        public ObservableCollection<FolderViewModel> Folders
        {
            get
            {
                return _folders;
            }
            set
            {
                _folders = value;
                this.OnPropertyChanged("Folders");
            }
        }
        #endregion //Folders

        #endregion //Presentation Members

        #region Public Methods

        #region AddFolder
        public void AddFolder(string title)
        {
            if (Database.AddFolder(title, 0) >= 0)
            {
                this.LoadFolders();
            }
        }
        #endregion //AddFolder

        #endregion //Public Methods

        #region Private Methods

        #region LoadFolders
        public void LoadFolders()
        {
            List<kFolder> folders = Database.GetFolders(null);
            this.Folders = new ObservableCollection<FolderViewModel>(
                (from folder in folders select new FolderViewModel(folder, this)).ToList()
            );
        }
        #endregion //LoadFolders

        #endregion //Private Methods
    }
}